package com.wande.dataplatform.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

import javax.sql.DataSource;

/**
 * 数据中台配置类
 *
 * @author ruoyi
 */
@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class DataPlatformConfig {

    private final DorisProperties dorisProperties;

    /**
     * 配置 Doris 数据源连接池
     */
    @Bean(name = "dorisDataSource")
    @ConditionalOnProperty(prefix = "doris", name = "jdbc-url")
    public DataSource dorisDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dorisProperties.getJdbcUrl());
        config.setUsername(dorisProperties.getUsername());
        config.setPassword(dorisProperties.getPassword());
        config.setMaximumPoolSize(dorisProperties.getMaxPoolSize());
        config.setMinimumIdle(dorisProperties.getMinIdle());
        config.setConnectionTimeout(dorisProperties.getConnectionTimeout());
        config.setPoolName("DorisHikariPool");
        
        // Doris 特定配置
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        
        return new HikariDataSource(config);
    }

    /**
     * 配置 RestTemplate 用于调用 DolphinScheduler API
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
